#include "TransformWidget.h"
#include "ui_TransformWidget.h"

#include "Object.h"


TransformWidget::TransformWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TransformWidget),
    m_object(NULL)
{
    ui->setupUi(this);
    setEnabled(false);
}

TransformWidget::~TransformWidget()
{
    delete ui;
}

void TransformWidget::setObject(Object *obj)
{
    m_object = obj;
    if(m_object)
        setEnabled(true);
    else
    {
        setEnabled(false);
        return;
    }

    Transform& transform = m_object->getTransform();

    Vector3f loc = transform.getTranslation();
    Vector3f rot = transform.getRotation();
    Vector3f scale = transform.getScale();

    ui->locX->blockSignals(true);
    ui->locX->setValue(loc.x);
    ui->locY->blockSignals(true);
    ui->locY->setValue(loc.y);
    ui->locZ->blockSignals(true);
    ui->locZ->setValue(loc.z);

    ui->rotX->blockSignals(true);
    ui->rotX->setValue(rot.x);
    ui->rotY->blockSignals(true);
    ui->rotY->setValue(rot.y);
    ui->rotZ->blockSignals(true);
    ui->rotZ->setValue(rot.z);

    ui->scaleX->blockSignals(true);
    ui->scaleX->setValue(scale.x);
    ui->scaleY->blockSignals(true);
    ui->scaleY->setValue(scale.y);
    ui->scaleZ->blockSignals(true);
    ui->scaleZ->setValue(scale.z);

    ui->locX->blockSignals(false);
    ui->locY->blockSignals(false);
    ui->locZ->blockSignals(false);
    ui->rotX->blockSignals(false);
    ui->rotY->blockSignals(false);
    ui->rotZ->blockSignals(false);
    ui->scaleX->blockSignals(false);
    ui->scaleY->blockSignals(false);
    ui->scaleZ->blockSignals(false);
}

void TransformWidget::translationChanged(double)
{
    m_object->getTransform().setTranslation(Vector3f(ui->locX->value(), ui->locY->value(), ui->locZ->value()));
    emit updated();
}

void TransformWidget::rotationChanged(double)
{
    m_object->getTransform().setRotation(Vector3f(ui->rotX->value(), ui->rotY->value(), ui->rotZ->value()));
    emit updated();
}

void TransformWidget::scaleChanged(double)
{
    m_object->getTransform().setScale(Vector3f(ui->scaleX->value(), ui->scaleY->value(), ui->scaleZ->value()));
    emit updated();
}
